# coding = UTF-8
# @Time : 2022/04/19 14:20
# @Author : PP_YY
# @File : 实验1.py
# @Description : 输入串x，输出所有前缀和后缀和所有子串
import numpy
import tkinter

top = tkinter.Tk()
top.title("字符串解析")
top.geometry('500x300')
top.resizable(0, 0)
entry_string = tkinter.Entry(top, bd=1,width=50)
show_data = tkinter.Text(top,bd=0,font=('等线', 12),highlightthickness=0,state="disable",width=60,height=10)


def add_null(out_list):
    out_list.append("null")


def print_ex(X):
    out_list=[]
    lens = len(X)
    for i in range(1,lens+1):
        out_list.append(X[0:i])
    add_null(out_list)
    print("所有前缀",out_list,"数量为：",len(out_list))
    #numpy.unique(out_list)
    return out_list


def print_la(X):
    out_list = []
    lens = len(X)
    for i in range(1,lens+1):
        out_list.append(X[lens-i:lens])
    add_null(out_list)
    print("所有后缀",out_list,"数量为：",len(out_list))
    #numpy.unique(out_list)
    return out_list


def print_all_sub(X):
    out_list = []
    lens = len(X)
    for i in range(lens+1):
        for j in range(i+1,lens+1):
            out_list.append(X[i:j])
    add_null(out_list)
    out_list = list(numpy.unique(out_list))
    print("所有子串",out_list,"数量为：",len(out_list))
    return out_list


def main():
    global entry_string,top,show_data
    show_data.config(state="normal")
    X = entry_string.get()
    ex_list=print_ex(X)
    la_list=print_la(X)
    sub = list(set(ex_list)|set(la_list))
    all_sub = print_all_sub(X)
    surplus = list(set(all_sub)-set(sub))
    #print("除去前后缀后子串",surplus,"数量为：",len(surplus))
    show_text='所有前缀：'+ ",".join(ex_list) + '\n数目为：' + str(len(ex_list)) + '\n所有后缀：' + ",".join(la_list) + '\n数目为：'+str(len(la_list)) + '\n除前缀后缀子串：'+','.join(surplus) +'\n数目为：'+str(len(surplus))
    show_data.delete('1.0','end')
    show_data.insert('1.0',show_text)
    show_data.config(state="disable")
    


def GUI_main():
    global entry_string,top,show_data
    notice_label = tkinter.Label(top, text="请输入需要解析的字符串", width=40, font=('等线', 15))
    start_button = tkinter.Button(top, text="解析", command=main, bd=1, font=('等线', 15))
    entry_string.place(x=15, y=40)
    start_button.place(x=360, y=31)
    notice_label.place(x=-118, y=10)
    #show_data.insert('1.0',"data\n")
    show_data.place(x=15,y=80)
    top.mainloop()


if __name__ == "__main__":
#    X = input("请输入子串")
#    
    GUI_main()